var $ = require('jquery');
var $footerTmp = $(require('./index.html'));
module.exports.create = function ($parent) {
  var $footer = $footerTmp.clone();
  var sendKey = $('body .set-send-key input[name=sendKey][checked]').val();
  if (sendKey == 'ctrlEnter') {
    $('.ctrl-enter-key').find('i').removeClass('im-icon-right').addClass('im-icon-check');
    $('.enter-key').find('i').removeClass('im-icon-check').addClass('im-icon-right');
  }
  limitInput($footer);
  changeSendKey($footer);
  $parent.append($footer);
  return $footer;
};

function limitInput($footer) {
  $footer.closest('.im-edit-area').find('.ipt-content').on('keyup', function () {
    var len = $(this).text().length;
    if (len <= 350) {
      $footer.find('.im-edit-tip').html('还可以输入<span class="still-count">' + (350 - len) + '</span>字');
      return;
    }
    $footer.find('.im-edit-tip').html('您输入的文字已经超过<span class="still-count">' + (len - 350) + '</span>字');
  });
}

// 切换发送按钮快捷键
function changeSendKey($footer) {
  $footer.find('.set-send-btn').on('click', function () {
    var $setKey = $footer.find('.set-send-key');
    if ($setKey.css('display') == 'none') {
      $setKey.css({'display': 'block'});
    } else {
      $setKey.css({'display': 'none'});
    }
    return false;
  });

  $footer.find('.set-send-label').on('click', function () {
    var $enter = $('.enter-key');
    var $ctrlEnter = $('.ctrl-enter-key');
    if ($(this).hasClass('enter-key')) {
      $enter.find('i').removeClass('im-icon-right').addClass('im-icon-check');
      $ctrlEnter.find('i').removeClass('im-icon-check').addClass('im-icon-right');
    } else {
      $ctrlEnter.find('i').removeClass('im-icon-right').addClass('im-icon-check');
      $enter.find('i').removeClass('im-icon-check').addClass('im-icon-right');
    }
    var $currentRadio = $(this).find('input[type=radio]');
    $currentRadio.attr('checked', true);
    $('body').trigger('set.send.key', $currentRadio.val());
    $footer.find('.set-send-key').hide();
  });
}
